package com.wanmait.wisdom.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.wisdom.entity.Admin;

import com.wanmait.wisdom.entity.Building;
import com.wanmait.wisdom.manage.service.ManageBuildingService;
import com.wanmait.wisdom.mapper.BuildingMapper;
import com.wanmait.wisdom.mapper.ResidentMapper;
import com.wanmait.wisdom.util.AjaxResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ManageBuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> implements ManageBuildingService {

    @Resource
    private BuildingMapper buildingMapper;

    @Resource
    private ResidentMapper residentMapper;
    @Override
    public AjaxResult listPage(Integer pageNum, Integer pageSize) {
        Page<Building> page =new Page<>(pageNum,pageSize);
        Page<Building> buildingPage=buildingMapper.selectPage(page,null);
        buildingPage.getRecords().forEach(building -> building.setResident(residentMapper.selectById(building.getBuildingManageId())));
        return AjaxResult.success("查询成功").put("buildingPage",buildingPage);
    }

    @Override
    public AjaxResult savebuilding(Building building) {
        save(building);
        return AjaxResult.success("添加成功");
    }

    @Override
    public AjaxResult checkName(Building building) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("building_name",building.getBuildingName());
        Building building1 = buildingMapper.selectOne(queryWrapper);
        return AjaxResult.success("查询成功").put("building",building1) ;
    }

    @Override
    public AjaxResult findBuilding() {
       List<Building> buildingList= buildingMapper.selectList(null);
        return AjaxResult.success("查询成功").put("buildingList",buildingList);
    }

    /**
     * 查询楼号列表，去重
     * @return
     */
    @Override
    public AjaxResult FindBuildingNumberOptions() {
        List<Building> strings= buildingMapper.findBuildingNumberOptions();
        return AjaxResult.success().put("findBuildingNumberOptions",strings);
    }

    @Override
    public Long getBuildingCount() {
        return buildingMapper.selectCount(null);
    }
}
